//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   CostStructureItemAction.java  $$
//* $$Revision:   1.1  $$
//* $$Author:   JLOPE262  $$
//* $$Date:   Nov 21 2008 14:26:32  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.view.action;

import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.facade.CostStructureFacade;
import ar.com.ford.it.fullEconomics.facade.EconomicIndexDefFacade;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc06Item;
import ar.com.ford.it.fullEconomics.view.form.CostStructureItemForm;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;
import ar.com.ford.it.webengine.facade.FacadeProvider;
import ar.com.ford.it.webengine.struts.AbstractBaseAction;
import ar.com.ford.it.webengine.struts.FordActionMapping;
import ar.com.ford.it.webengine.websession.WebSession;

public class CostStructureItemAction extends AbstractBaseAction {

    /**
     * Setea el mensaje de "busqueda sin resultados" cuando la busqueda no trae
     * resultados
     * 
     * @param request
     * Request donde se guarda el mensaje
     */
    private void searchWithNoResults(HttpServletRequest request) {
        ActionMessages am = new ActionMessages();

        am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                "message.noResults"));
        saveMessages(request, am);
    }

    /**
     * Setea el mensaje de error debido a que un item no puede ser aprobado por
     * el mismo usuario que lo propuso
     * 
     */
    private void setErrorItemCannotBeApproved(HttpServletRequest request) {
        ActionMessages am = new ActionMessages();

        am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                "costStructure.item.error.approve.sameUser"));
        saveErrors(request, am);
    }

    /**
     * Resetea el formulario y envia a la pagina principal de Item Definition
     * 
     */
    public ActionForward initPage(FordActionMapping mapping,
                                  ActionForm actionForm,
                                  HttpServletRequest request,
                                  HttpServletResponse response,
                                  WebSession webSession, ActionMessages msgs,
                                  ActionMessages warns, ActionMessages errorMsgs) {
        CostStructureItemForm form = (CostStructureItemForm) actionForm;
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        CostStructureFacade costStructureFacade = (CostStructureFacade) FacadeProvider
                .getFacade(request, CostStructureFacade.class);
        EconomicIndexDefFacade economicFacade = (EconomicIndexDefFacade) FacadeProvider
                .getFacade(request, EconomicIndexDefFacade.class);

        form.resetForm();
        form.setScreen("list");
        try {
            form.setEconomics(economicFacade.findAllEconomicIndex(user
                    .getTeesg01Plant()));
            request.getSession().setAttribute("economics", form.getEconomics());
            form.setItemCategories(costStructureFacade
                    .findAllItemCategories(user.getTeesg01Plant()));
        } catch (FullEconomicsException e) {
            ActionMessages am = new ActionMessages();
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }
        request.getSession().setAttribute("itemCategories",
                form.getItemCategories());

        return mapping.findForward("success");
    }

    /**
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward findCostStructureItem(FordActionMapping mapping,
                                               ActionForm actionForm,
                                               HttpServletRequest request,
                                               HttpServletResponse response,
                                               WebSession webSession,
                                               ActionMessages msgs,
                                               ActionMessages warns,
                                               ActionMessages errorMsgs) {

        CostStructureItemForm form = (CostStructureItemForm) actionForm;
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        CostStructureFacade facade = (CostStructureFacade) FacadeProvider
                .getFacade(request, CostStructureFacade.class);
        Collection<Teesc06Item> items = null;

        form.clearCreateUpdateFields();
        try {
            items = facade.findItems(form.getItemCatCodeSelFind(), form
                    .getCodeFind(), form.getNameFind(), user.getTeesg01Plant());
        } catch (FullEconomicsException e) {
            ActionMessages am = new ActionMessages();
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }
        form.setItems(items);
        if (items.size() == 0) {
            searchWithNoResults(request);
        } else if (items.size() == 1) {
            Teesc06Item item = items.iterator().next();
            form.setItemDetails(item);

            // Por si decide hacer un update
            form.setItemSelected(item);
            form.setScreen("detail");
        } else {
            form.setScreen("list");
        }

        return mapping.findForward("success");
    }

    /**
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward createUpdateCostStructureItem(
                                                       FordActionMapping mapping,
                                                       ActionForm actionForm,
                                                       HttpServletRequest request,
                                                       HttpServletResponse response,
                                                       WebSession webSession,
                                                       ActionMessages msgs,
                                                       ActionMessages warns,
                                                       ActionMessages errorMsgs) {
        CostStructureItemForm form = (CostStructureItemForm) actionForm;
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        CostStructureFacade facade = (CostStructureFacade) FacadeProvider
                .getFacade(request, CostStructureFacade.class);

        try {
            if (form.getCodeCreateUpdate() == null) {
                facade.createItem(form.getItemForCreate(), form
                        .getItemCatCodeSel(), form.getEconomicCodeSel(), user);

            } else {
                facade.updateItem(
                        form.getItemForUpdate(form.getItemSelected()), form
                                .getItemCatCodeSel(),
                        form.getEconomicCodeSel(), user);
            }
        } catch (FullEconomicsException e) {
            ActionMessages am = new ActionMessages();
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }

        return mapping.findForward("success");
    }

    /**
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward proposeCostStructureItem(FordActionMapping mapping,
                                                  ActionForm actionForm,
                                                  HttpServletRequest request,
                                                  HttpServletResponse response,
                                                  WebSession webSession,
                                                  ActionMessages msgs,
                                                  ActionMessages warns,
                                                  ActionMessages errorMsgs) {
        CostStructureItemForm form = (CostStructureItemForm) actionForm;
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        CostStructureFacade facade = (CostStructureFacade) FacadeProvider
                .getFacade(request, CostStructureFacade.class);

        if (facade.itemCanBeProposed(form.getItemSelected())) {
            try {
                facade.proposeItem(form.getItemSelected(), user);
            } catch (FullEconomicsException e) {
                ActionMessages am = new ActionMessages();
                am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                        .getMessageResource()));
                saveErrors(request, am);
            }
        }
        return mapping.findForward("success");
    }

    /**
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward approveCostStructureItem(FordActionMapping mapping,
                                                  ActionForm actionForm,
                                                  HttpServletRequest request,
                                                  HttpServletResponse response,
                                                  WebSession webSession,
                                                  ActionMessages msgs,
                                                  ActionMessages warns,
                                                  ActionMessages errorMsgs) {
        CostStructureItemForm form = (CostStructureItemForm) actionForm;
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        CostStructureFacade facade = (CostStructureFacade) FacadeProvider
                .getFacade(request, CostStructureFacade.class);

        if (facade.itemCanBeApproved(form.getItemSelected(), user)) {
            try {
                facade.approveItem(form.getItemSelected(), user);
            } catch (FullEconomicsException e) {
                ActionMessages am = new ActionMessages();
                am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                        .getMessageResource()));
                saveErrors(request, am);
            }
        } else {
            this.setErrorItemCannotBeApproved(request);
        }
        return mapping.findForward("success");
    }

    /**
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward newCostStructureItem(FordActionMapping mapping,
                                              ActionForm actionForm,
                                              HttpServletRequest request,
                                              HttpServletResponse response,
                                              WebSession webSession,
                                              ActionMessages msgs,
                                              ActionMessages warns,
                                              ActionMessages errorMsgs) {
        CostStructureItemForm form = (CostStructureItemForm) actionForm;
        form.clearCreateUpdateFields();
        form.setScreen("detail");

        return mapping.findForward("success");
    }

    /**
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward deleteCostStructureItem(FordActionMapping mapping,
                                                 ActionForm actionForm,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response,
                                                 WebSession webSession,
                                                 ActionMessages msgs,
                                                 ActionMessages warns,
                                                 ActionMessages errorMsgs) {
        CostStructureItemForm form = (CostStructureItemForm) actionForm;
        CostStructureFacade facade = (CostStructureFacade) FacadeProvider
                .getFacade(request, CostStructureFacade.class);

        try {
            facade.deleteItem(form.getItemSelected());
        } catch (FullEconomicsException e) {
            ActionMessages am = new ActionMessages();
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }
        return mapping.findForward("success");
    }

    /**
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward editCostStructureItem(FordActionMapping mapping,
                                               ActionForm actionForm,
                                               HttpServletRequest request,
                                               HttpServletResponse response,
                                               WebSession webSession,
                                               ActionMessages msgs,
                                               ActionMessages warns,
                                               ActionMessages errorMsgs) {
        CostStructureItemForm form = (CostStructureItemForm) actionForm;

        form.setItemDetails(form.getItemSelected());
        form.setScreen("detail");
        return mapping.findForward("success");
    }

    /**
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward cleanFindCostStructureItem(
                                                    FordActionMapping mapping,
                                                    ActionForm actionForm,
                                                    HttpServletRequest request,
                                                    HttpServletResponse response,
                                                    WebSession webSession,
                                                    ActionMessages msgs,
                                                    ActionMessages warns,
                                                    ActionMessages errorMsgs) {
        CostStructureItemForm form = (CostStructureItemForm) actionForm;

        form.cleanFindFields();
        return mapping.findForward("success");
    }
}
